

import 'package:furaha_mkopo/commonUI/fm_progress_hud.dart';
import 'package:furaha_mkopo/commonUI/fm_toast.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'fm_order_api.dart';
import 'fm_order_model.dart';

class FMOrderController extends GetxController {

  RefreshController? refreshController;

  bool isEmpty = false;

  List<FMOrderBeamedModel>? dataSource;

  void refreshData(String listId) {
    FMOrderApi api = FMOrderApi(hesitated: listId);
    api.send().then((value){
      print(value);
    });
    api.send<FMOrderModel,FMOrderModel>(decodeType: FMOrderModel()).then((value){
      FMProgressHud.hideLoading();
      if (value.code == 0) {
        if (value.data?.beamed == null || value.data!.beamed!.isEmpty) {
          isEmpty = true;
        }else {
          isEmpty = false;
          dataSource = value.data?.beamed;
        }
        update();
        refreshController!.refreshCompleted();
      }else {
        FMToast.showText(value.message ?? "Network Connection Error");
      }
    });
  }

  int get numberOfItems {
    return dataSource?.length ?? 0;
  }

  String statusTitleAtIndex(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    return model?.passing ?? "";
  }

  String orderImageAtIndex(int index) {
    if (index == 0) {
      return numberOfItems == 1 ? "assets/order/order_cell_bg.png" : "assets/order/order_swear_bottom.png";
    }else if (index == numberOfItems - 1){
      return "assets/order/order_swear_top.png";
    }else {
      return "assets/order/order_swear_middle.png";
    }
  }

  String logoAtIndex(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    return model?.felt ?? "";
  }

  String titleAtIndex(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    return model?.attempt ?? "";
  }

  String priceTitle(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    List<FMOrderSubListModel>? myself = model?.myself;
    if (myself != null && myself.isNotEmpty) {
      FMOrderSubListModel m = myself.first;
      return m.soul ?? "";
    }else {
      return "";
    }
  }

  String priceContent(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    List<FMOrderSubListModel>? myself = model?.myself;
    if (myself != null && myself.isNotEmpty) {
      FMOrderSubListModel m = myself.first;
      return m.patiently ?? "";
    }else {
      return "";
    }
  }

  List<FMOrderSubListModel> subList(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    List<FMOrderSubListModel>? myself = model?.myself;

    if (myself != null && myself.isNotEmpty) {
      List<FMOrderSubListModel> totalList = myself.map((e){
        return e;
      }).toList();
      totalList.removeAt(0);

      // 添加协议
      if (hasProtocol(index)) {
        FMOrderBeamedModel? model = beamedModelAtIndex(index);
        FMOrderSubListModel m = FMOrderSubListModel(
          soul: model!.manner,
          patiently: "check>"
        );
        totalList.add(m);

      }
      return totalList;
    }else {
      return [];
    }
  }

  String orderCircleImageName(int index) {
    List<FMOrderSubListModel> list = subList(index);
    return "assets/order/order_cell_${list.length <= 3 ? "less_circle" : "circle"}.png";
  }

  bool hasProtocol(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    return model?.manner != null && model!.manner!.isNotEmpty;
  }

  String protocolUrl(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    return model?.mother ?? "";
  }

  String jumpUrlAtIndex(int index) {
    FMOrderBeamedModel? model = beamedModelAtIndex(index);
    return model?.future ?? "";
  }

  FMOrderBeamedModel? beamedModelAtIndex(int index) {
    if (dataSource == null) return null;

    if (index >= dataSource!.length) return null;

    return dataSource![index];
  }


}